package cvi.com.reader.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.orhanobut.hawk.Hawk;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.adapter.ClassGridViewAdapter;
import cvi.com.reader.base.BaseActivity;
import cvi.com.reader.bean.ClassBean;
import cvi.com.reader.bean.ClassDetailBean;
import cvi.com.reader.bean.LoginBean;
import cvi.com.reader.bean.LonginBeanNew;
import cvi.com.reader.bean.ScanCodeBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.network.DefaultObserver;
import cvi.com.reader.network.NetWorkManager;
import cvi.com.reader.tool.UtilsOnclickListen;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by WP on 2020/7/17.
 * 登录界面
 */

public class LoginActivityNew extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.gridview)
    GridView gridview;
    @BindView(R.id.spinner)
    Spinner spinner;

    ClassGridViewAdapter classGridViewAdapter;
    List<ClassDetailBean.DataBean> dataBeanList = new ArrayList<>();
    List<String> stringList = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private int classId = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_new;
    }

    @Override
    protected void initView() {
        login.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        getClassInfo();
        classGridViewAdapter = new ClassGridViewAdapter(LoginActivityNew.this, dataBeanList);
        gridview.setAdapter(classGridViewAdapter);
        classGridViewAdapter.notifyDataSetChanged();

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //该方法表示当spinner中的item被选中是回调的方法

            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = adapter.getItem(position);//根据下标从适配器中获取内容
                getClasslistInfo(s1);
            }

            //该方法表示没有选择的时候回调的方法，这里不用去声明干什么

            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                classGridViewAdapter.setSeclection(i);//传值更新i
                classGridViewAdapter.notifyDataSetChanged();
                ClassDetailBean.DataBean dataBean = (ClassDetailBean.DataBean) adapterView.getAdapter().getItem(i);
                classId = (int) dataBean.getId();
            }
        });

    }

    @Override
    protected void extrasDoSome(Bundle extras) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                //登录
                if (UtilsOnclickListen.isFastClick()) {
                    Login(classId);
                }
                break;
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    private void Login(int classId) {

        NetWorkManager.getInstance().getNetWorkApiService().getsLogin(classId + "").compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(LoginActivityNew.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            LonginBeanNew longinBeanNew = response.toJavaObject(LonginBeanNew.class);
                            if (longinBeanNew.getCode() == 1) {
                                Hawk.put("longinBeanNew",longinBeanNew);
                                constant.user_id = longinBeanNew.getMsg().getId();
                                Intent intent = new Intent(LoginActivityNew.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }

                        }


                    }
                });
    }

    private void getClassInfo() {
        Log.d("hgrhrhrh111", "gddf");
        NetWorkManager.getInstance().getNetWorkApiService().getClassInfo().compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(LoginActivityNew.this) {
                    @Override
                    protected void onFail(JSONObject response) {
                        super.onFail(response);
                        Log.d("hgrhrhrh222", response.toString());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        Log.d("hgrhrhrh333", e.toString());
                    }

                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            Log.d("hgrhrhrh444", response.toString());
                            stringList.clear();
                            ClassBean classBean = response.toJavaObject(ClassBean.class);
                            if (classBean.getData() != null && classBean.getData().size() > 0) {
                                stringList.addAll(classBean.getData());
                                adapter = new ArrayAdapter<String>(LoginActivityNew.this, android.R.layout.simple_spinner_dropdown_item, stringList);
                                spinner.setAdapter(adapter);
                                spinner.setSelection(0);
                                adapter.notifyDataSetChanged();
                            }

                        }

                    }
                });
    }

    private void getClasslistInfo(String className) {

        NetWorkManager.getInstance().getNetWorkApiService().getClasslistInfo(className).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(LoginActivityNew.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {
                        if (response != null) {
                            ClassDetailBean classDetailBean = response.toJavaObject(ClassDetailBean.class);
                            if (classDetailBean.getData() != null && classDetailBean.getData().size() > 0) {
                                classGridViewAdapter.setData(classDetailBean.getData(), -1);
                                classGridViewAdapter.notifyDataSetChanged();
                            }

                        }

                    }
                });
    }

    private void setChangeIp() {
        Map<String, Object> map = new HashMap<>();
        NetWorkManager.getInstance().getNetWorkApiService().login(map).compose(this.<JSONObject>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<JSONObject>(LoginActivityNew.this) {
                    @Override
                    protected void onSuccess(JSONObject response) {

                    }
                });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

}

